package com.kristou.urgdriver.service;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.os.Binder;
import android.os.IBinder;

import android.R;
import android.util.Log;

import com.kristou.urgdriver.connection.SerialConnection;
import com.kristou.urgdriver.sensor.RangeSensorInformation;
import com.kristou.urgdriver.sensor.UrgDevice;
import com.kristou.urgdroid.MainActivity;

/**
 * Created by j601s03 on 13/10/08.
 */
public class CaptureService extends Service {

    private static final String TAG = CaptureService.class.getSimpleName();

    public static final String ACTION_DISCONNECT = "com.kristou.urgdriver.service.ACTION_DISCONNECT";
    public static final String ACTION_CONNECT = "com.kristou.urgdriver.service.ACTION_CONNECT";

    private final IBinder mBinder = new CaptureServiceBinder();
    NotificationManager mNotifier;

    private SerialConnection mSerial;
    private UrgDevice mDevice = new UrgDevice();

    // Unique Identification Number for the Notification.
    // We use it on Notification start, and to cancel it.
    private int NOTIFICATION = 0;

    public static final int MSG_GET_VV = 1;
    public static final int MSG_GET_II = 2;
    public static final int MSG_GET_PP = 3;

    @Override
    public void onCreate() {
        super.onCreate();
        mNotifier = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        mSerial = new SerialConnection((UsbManager) getSystemService(Context.USB_SERVICE));

        // [FTDriver] setPermissionIntent() before begin()
        PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
                SerialConnection.ACTION_USB_PERMISSION), 0);
        mSerial.setPermissionIntent(permissionIntent);

        mDevice.setConnection(mSerial);

        mDevice.connect();

        // Display a notification about us starting.  We put an icon in the status bar.
        showNotification();
        Log.i(TAG, "Capture Service Created");
    }

    public boolean isSensorConnected() {
        return mDevice.isConnected();
    }

    public RangeSensorInformation getInformation() {
        if (mDevice.isConnected()) {
            return mDevice.getInformation();
        } else {
            return null;
        }
    }

    private void showNotification() {
        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);

        PendingIntent disconnectActionIntent = PendingIntent.getService(this, 0, new Intent(this, CaptureService.class).setAction(ACTION_DISCONNECT), 0);
        PendingIntent connectActionIntent = PendingIntent.getService(this, 0, new Intent(this, CaptureService.class).setAction(ACTION_CONNECT), 0);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification.Builder(this)
                .setContentTitle("URG Capture Service")
                .setContentText("Service Started")
                .setSmallIcon(R.drawable.ic_popup_sync)
                .setContentIntent(contentIntent)
                .addAction(R.drawable.ic_menu_add, "Connect", connectActionIntent)
                .addAction(R.drawable.ic_menu_delete, "Disconnect", disconnectActionIntent).build();


        // Send the notification.
        mNotifier.notify(NOTIFICATION, notification);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "Received start id " + startId + ": " + intent);
        String action = intent.getAction();
        if (ACTION_DISCONNECT.equals(action)) {
            mDevice.disconnect();
            Log.i(TAG, "Disconnected by intent");
        } else if (ACTION_CONNECT.equals(action)) {
            mDevice.connect();
            Log.i(TAG, "Connected by intent");
        } else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action) ||
                SerialConnection.ACTION_USB_PERMISSION.equals(action)) {
            mSerial.attach(intent);
            mDevice.connect();
        } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            mSerial.detach(intent);
            mDevice.disconnect();
        }
        return Service.START_STICKY;
    }

    public class CaptureServiceBinder extends Binder {
        public CaptureService getService() {
            return CaptureService.this;
        }
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
